<?php
/**
 * CreateTime: 2023/8/22 17:20
 */

namespace App\Http\Controllers\MeSendLetter;

use App\Consts\MeLawCaseConsts\MeLawCaseLetterConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Requests\ListBasicRequest;
use App\Models\LatterCache\MeSendLatterCacheModel;
use App\Models\MeClient\MeClientModel;

class MeSendLetterController
{
    public function getStaffLetterList(ListBasicRequest $request){
        $pFilters = $request->get('filters');
        $pageSize = $request->get('page_size');
        $filters = $pFilters ? json_decode($pFilters, true) : [];
        $query = (new MeSendLatterCacheModel());
        $query = $query->select([
            "send_letter_cache_id",
            "company_id",
            "company_id as company_name_str",
//            "letter_ids",
            "letter_status",
            "letter_count",
            "letter_type",
            "letter_status as letter_status_str ",
            "client_id"
        ])->where('type',2);
        if($filters) {
            if (isset($filters['client_id']) && $filters['client_id']) {
                $query = $query->where('client_id', $filters['client_id']);
            }
        }

        $clientModel = new MeClientModel();


        $data = (new MeSendLatterCacheModel())->getListNoTimeNotStatus($query,$pageSize)->toArray() ?? [];
        foreach($data['data'] as $key=>$value){
            $data['data'][$key]['client_name'] = $clientModel->getByIdNotCompany($value['client_id'],['client_name'])['client_name'] ?? "-";
            $data['data'][$key]['letter_status'] = $this->getLetterStatus($value['letter_status']);
            $data['data'][$key]['letter_type_str'] = MeLawCaseLetterConsts::getLetterTypeByType($value['letter_type']);
        }

        return ResponseFunc::returnJson(
            'ok',
            $data,
            '获取留言板成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    private function getLetterStatus($status){
        switch ($status){
            case 1:
                return "审核中";
            case 2:
                return "已审核";
            case 3:
                return "纸质函生成中";
            case 4:
                return "纸质函生成完成";
        }
    }

}